<?php
class Test_Flow extends TestCase {
	public function setup() {

	}

	// 一連の流れ
	public function test_flow_ok() {
		$uuid = 'test'.rand(100000,999999999);
		
		// 会員認証
		$response = $this->curl('api/member/auth/',
			array(
				'uuid' => $uuid,
				'password' => '6d022a1256cef754c635b81fe838d5e1',
				'identification' => md5(rand(0,9999999)),
				)
		);
		$this -> assertEquals(201, $response -> status);
		$json = json_decode($response->body,true);
		$member_id = $json['member_id'];
		$access_token = $json['access_token'];
		
		// 会員更新
		$response = $this->curl('api/member/post/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'sex' => '1',
				'birth_date' => '1978/10/10',
				'area' => '13',
				'push_enable' => '1',
				)
		);
		$this -> assertEquals(201, $response -> status);
		$json = json_decode($response->body,true);
				
		// 会員取得
		$response = $this->curl('api/member/get/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				)
		);
		$this -> assertEquals(201, $response -> status);
		$json = json_decode($response->body,true);
		$this -> assertEquals('1', $json['sex']);
		$this -> assertEquals('1978/10/10', $json['birth_date']);
		$this -> assertEquals('13', $json['area']);
		
		// 会員成果
		$response = $this->curl('api/member/outcome/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'key1' => 'twitter_follow',
				)
		);
		$this -> assertEquals(201, $response -> status);
		
		// AppDriver連携
	 	$response = $this->curl('api/ad/appdriver/',
			array(
				'identifier' => $member_id,
				'achieve_id' => 'test'.rand(0,100),
				'campaign_id' => '4854',
				'point' => rand(100,999),
				),
			'GET'
		);
		$this -> assertEquals(201, $response -> status);

		// SMAAD連携
	 	$response = $this->curl('api/ad/smaad/',
			array(
				'user' => $member_id,
				'adid' => '67504621',
				'net' => rand(100,999),
				),
			'GET'
		);
		$this -> assertEquals(200, $response -> status);

		// CAリワード連携
	 	$response = $this->curl('api/ad/ca/',
			array(
				'uid' => $member_id,
				'cid' => '6611',
				'pid' => '1',
				'action_date' => '20130101',
				'point' => rand(100,999),
				'amount' => rand(100,999),
				),
			'GET'
		);
		$this -> assertEquals(201, $response -> status);

		// affilead連携
	 	$response = $this->curl('api/ad/affilead/',
			array(
				'user' => $member_id,
				'ad' => '12983',
				'point' => rand(100,999),
				),
			'GET'
		);
		$this -> assertEquals(201, $response -> status);

		// ランキング収録 累計
		$response = $this->curl('api/ranking/get/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'kind' => '2',
				'period' => '3',
				)
		);
		$this -> assertEquals(200, $response -> status);
		$songs = json_decode($response->body,true);
		shuffle($songs);
		
		// 新規曲
		$songs[0]['song_title'] = 'test'.rand(0,1000000);
		$songs[0]['artist'] = 'test'.rand(0,1000000);
		
		// コメント投稿
		$words = array('キター','８８８８８８８','泣ける','これいいよね','サビがさいこう','くさい');
		for($i = 0;$i < 1;$i ++) {
			shuffle($songs);
			$song_title = $songs[0]['song_title'];
			$artist = $songs[0]['artist'];
			shuffle($words);
			$word = 'test'.$words[0];
			$seconds = rand(0,180);
			$response = $this->curl('api/comment/post/',
				array(
					'member_id' => $member_id,
					'access_token' => $access_token,
					'song_title' => $song_title,
					'artist' => $artist,
					'word1' => $word,
					'seconds1' => $seconds,
					)
			);
			$this -> assertEquals(201, $response -> status);
		}

		// コメント取得 投稿確認
		$response = $this->curl('api/comment/get/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'song_title' => $song_title,
				'artist' => $artist,
				'play' => '1',
				)
		);
		$this -> assertEquals(200, $response -> status);
		$json = json_decode($response->body,true);
		$this -> assertEquals($word, $json[0]['word']);

		// コメント通報
		$response = $this->curl('api/comment/report/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'comment_id' => $json[0]['comment_id'],
				'word' => 'test通報理由',
				)
		);
		$this -> assertEquals(201, $response -> status);

		// お気に入り登録
		for($i = 1; $i <= 2; $i ++) {
	  		$response = $this->curl('api/favorite/post/',
				array(
					'member_id' => $member_id,
					'access_token' => $access_token,
					'song_title' => $songs[$i]['song_title'],
					'artist' => $songs[$i]['artist'],
					)
			);
			$this -> assertEquals(201, $response -> status);
		}

		// 曲情報取得
		$response = $this->curl('api/song/get/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'song_title' => $songs[1]['song_title'],
				'artist' => $songs[1]['artist'],
			)
		);
		$this -> assertEquals(201, $response -> status);
		$json = json_decode($response->body,true);
		$this -> assertEquals('1', $json['favorite']);
		
		// お気に入り一覧
		$response = $this->curl('api/favorite/get/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
			)
		);
		$this -> assertEquals(201, $response -> status);
		$json = json_decode($response->body,true);
		$this -> assertEquals($songs[2]['song_title'], $json[0]['song_title']);
		
		// お気に入り削除
	 	$response = $this->curl('api/favorite/delete/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'song_title' => $songs[1]['song_title'],
				'artist' => $songs[1]['artist'],
				)
		);
		$this -> assertEquals(201, $response -> status);

		// 検索
		for($i = 0;$i < 3;$i ++) {
			for($j = 0;$j < 3;$j ++) {
				// 検索件数
			 	$response = $this->curl('api/search/facet/',
					array(
						'member_id' => $member_id,
						'access_token' => $access_token,
						'kind' => $j,
						'word' => 'き',
						)
				);
				$this -> assertEquals(200, $response -> status);

				// 楽曲検索
			 	$response = $this->curl('api/search/song/',
					array(
						'member_id' => $member_id,
						'access_token' => $access_token,
						'kind' => $j,
						'word' => 'き',
						)
				);
				$this -> assertEquals(200, $response -> status);

				// アーティスト検索
			 	$response = $this->curl('api/search/artist/',
					array(
						'member_id' => $member_id,
						'access_token' => $access_token,
						'kind' => $j,
						'word' => 'き',
						)
				);
				$this -> assertEquals(200, $response -> status);
			}
			
			// iTunes検索
		 	$response = $this->curl('api/search/itunes/',
				array(
					'member_id' => $member_id,
					'access_token' => $access_token,
					'word' => 'き',
					)
			);
			$this -> assertEquals(200, $response -> status);
		}
		$json = json_decode($response->body,true);
		shuffle($json);

		// 会員リクエスト
		$response = $this->curl('api/member/request/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				'key1' => 'itunes_song',
				'key2' => $json[0]['itunes_track_id'],
				'key3' => $json[0]['song_title'],
				'key4' => $json[0]['artist'],
				'key5' => $json[0]['link'],
				)
		);
		$this -> assertEquals(201, $response -> status);

		// ダウンロード一覧
		$response = $this->curl('api/member/downloadlist/',
			array(
				'member_id' => $member_id,
				'access_token' => $access_token,
				)
		);
		$this -> assertEquals(201, $response -> status);

		// デバイストークン
		$response = $this->curl('api/etc/iosdevicetoken/',
			array(
				'uuid' => $uuid,
				'device' => 'caa9c4b60317393d2fa77d4c9512d7e579c892c97009d8655bd016e911588a7e',
				)
		);
		$this -> assertEquals(201, $response -> status);
				
		// 負荷 コメント取得
		for($i = 0;$i < 10;$i ++) {
			shuffle($songs);
			$song_title = $songs[0]['song_title'];
			$artist = $songs[0]['artist'];
			$response = $this->curl('api/comment/get/',
				array(
					'member_id' => $member_id,
					'access_token' => $access_token,
					'song_title' => $song_title,
					'artist' => $artist,
					)
			);
			$this -> assertEquals(200, $response -> status);
			$json = json_decode($response->body,true);
		}
		
		// 負荷ランキング取得
		for($i = 0; $i <=3; $i ++) {
			for($j = 0; $j <=3; $j ++) {
				$response = $this->curl('api/ranking/get/',
					array(
						'member_id' => $member_id,
						'access_token' => $access_token,
						'kind' => $i,
						'period' => $j,
						)
				);
				$this -> assertEquals(200, $response -> status);
				$songs = json_decode($response->body,true);
			}
		}

		// 負荷 会員認証
		for($i = 0;$i < 5;$i ++) {
			$response = $this->curl('api/member/auth/',
				array(
					'uuid' => $uuid,
					'password' => '6d022a1256cef754c635b81fe838d5e1',
					'identification' => md5(rand(0,9999999)),
					)
			);
			$this -> assertEquals(201, $response -> status);
		}

		// バナークリック
	 	$response = $this->curl('analyze/banner/?p=ios&q=iMobile',
			array()
		);
		$this -> assertEquals(301, $response -> status);
				
		// インストール成果送信
	 	$response = $this->curl('analyze/install/?p=ios&q=AppDriver&i=1',
			array()
		);
		$this -> assertEquals(301, $response -> status);
				
	}

	// 通信
	public function curl($uri,$post,$method='POST') {
		return Request::forge(
			//'https://commesodo.gtlab.jp/' . $uri,
			'http://dev01.gtlab.jp:50101/' . $uri,
			array(
				'driver' => 'curl',
				'options' => array(
					CURLOPT_FOLLOWLOCATION => false,
					CURLOPT_MAXREDIRS => 0,
					),
				)
			)
			-> set_method($method)
			-> execute($post)
			-> response();
	}
}
